<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资料类型" prop="dataId">
        <el-cascader
          clearable
          filterable
          :props="{expandTrigger: 'hover', value: 'id', label: 'dataName', disabled: 'disabled', 'emitPath': false, 'checkStrictly': true}"
          v-model="queryParams.dataId"
          :options="aDataList"
          :show-all-levels="true"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="标题名称" prop="daTitle">
        <el-input
          v-model="queryParams.daTitle"
          placeholder="请输入标题名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="权限" prop="agentLevel">
        <el-select v-model="queryParams.agentLevel" placeholder="请选择权限">
          <el-option
            v-for="dict in dict.type.agent_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间" prop="params">
        <el-date-picker
          v-model="dataPicker"
          type="daterange"
          value-format="yyyy-MM-dd"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:aDataInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:aDataInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:aDataInfo:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aDataInfoList" @selection-change="handleSelectionChange" :key="updateTable" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题名称" align="center" prop="daTitle" width="200" show-overflow-tooltip />
      <el-table-column label="上传信息" align="center" prop="infoImage" width="200" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.daCarousel" :showImgNum="3" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center" prop="daDetails" width="200" >
        <template slot-scope="scope">
          <div v-html="scope.row.daDetails" v-sub="30" v-copy></div>
        </template>
      </el-table-column>
      <el-table-column label="资料分类" align="center" prop="dataName" />
      <el-table-column label="点赞" align="center" prop="likeNum" width="50" />
      <el-table-column label="下载" align="center" prop="downloadNum" width="50" />
      <el-table-column label="收藏" align="center" prop="collectionNum" width="50" />
      <el-table-column label="分享" align="center" prop="shareNum" width="50" />
      <el-table-column label="权限" align="center" prop="agentLevel" width="90">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.agent_level" :value="scope.row.agentLevel"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="资料库ID" align="center" prop="dataId" />-->
      <el-table-column label="是否上线" align="center" prop="isShow" width="140">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isShow" @change="handleSwitchChange(scope.row)"  active-text="上线" inactive-text="下线" />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="numSoft" width="50" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{scope.row.createTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="getUserOperationList(scope.row)"
          >查看下载数</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:aDataInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:aDataInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改素材库详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="资料类型" prop="dataId">
          <el-cascader
            clearable
            filterable
            :props="{expandTrigger: 'hover', value: 'id', label: 'dataName', disabled: 'disabled', 'emitPath': false, 'checkStrictly': true}"
            v-model="form.dataId"
            :options="aDataList"
            :show-all-levels="true"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="权限" prop="agentLevel">
          <el-select v-model="form.agentLevel" placeholder="请选择权限">
            <el-option
              v-for="dict in dict.type.agent_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="点赞数" prop="likeNum">-->
        <!--          <el-input v-model="form.likeNum" placeholder="请输入点赞数" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="下载数" prop="downloadNum">-->
        <!--          <el-input v-model="form.downloadNum" placeholder="请输入下载数" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="收藏数" prop="collectionNum">-->
        <!--          <el-input v-model="form.collectionNum" placeholder="请输入收藏数" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="分享数" prop="shareNum">-->
        <!--          <el-input v-model="form.shareNum" placeholder="请输入分享数" />-->
        <!--        </el-form-item>-->
        <el-form-item label="是否上线" prop="isShow">
          <el-switch v-model="form.isShow" active-text="上线" inactive-text="下线" />
        </el-form-item>
        <el-form-item label="排序" prop="numSoft">
          <el-input-number v-model="form.numSoft" :min="0" :max="9999" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="标题名称" prop="daTitle">
          <el-input v-model="form.daTitle" placeholder="请输入标题名称" />
        </el-form-item>
        <el-form-item label="素材文件" prop="daCarousel">
          <image-upload v-model="form.daCarousel" :limit="1" :fileSize="2000" v-if="isFile === undefined" />
          <image-upload v-model="form.daCarousel" :limit="1" :fileSize="2000" :file-type="['pdf','doc','docx','xls','xlsx']" v-else-if="isFile" />
          <image-upload v-model="form.daCarousel" :limit="10" :fileSize="2000" :file-type="['png','jpg','jpeg','mp4','mpv']" v-else />
        </el-form-item>
        <el-form-item label="内容" prop="daDetails">
          <editor v-model="form.daDetails"  :min-height="192" :toolbar="[['emoji']]"></editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="downTitle" :visible.sync="downFlag" width="50%" append-to-body>
      <el-form :model="userParams" ref="userForm" size="small" :inline="true" v-show="showUserSearch" label-width="68px">
        <el-form-item label="昵称" prop="nickName">
          <el-input
            v-model="userParams.nickName"
            placeholder="请输入昵称"
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="userParams.phone"
            placeholder="请输入手机号"
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleUserQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetUserQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="UserOperationList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="昵称" align="center" prop="nickName" />
        <el-table-column label="头像" align="center" prop="headImg" >
          <template slot-scope="scope">
            <image-preview :src="scope.row.headImg" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="手机号" align="center" prop="phone" />
        <el-table-column label="创建时间" align="center" prop="createTime"/>
        <el-table-column label="下载数" align="center" prop="downNum" />
      </el-table>

      <pagination
        v-show="totals>0"
        :total="totals"
        :page.sync="userParams.pageNum"
        :limit.sync="userParams.pageSize"
        @pagination="getUserOperationList"
      />

    </el-dialog>
  </div>
</template>

<script>
import { listADataInfo, getADataInfo, delADataInfo, addADataInfo, updateADataInfo } from "@/api/system/aDataInfo";
import {listAData} from "@/api/system/aData";
import {handleTree, isFile} from "@/utils/ruoyi";
import { getUserOperList } from "@/api/system/aUser";

export default {
  name: "ADataInfo",
  dicts: ['agent_level'],
  data() {
    return {
      //dataId:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 素材库详情表格数据
      aDataInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        daTitle: null,
        daCarousel: null,
        daDetails: null,
        agentLevel: null,
        likeNum: null,
        downloadNum: null,
        collectionNum: null,
        shareNum: null,
        dataId: null,
        isShow: null,
        numSoft: null,
        params:{}
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        daTitle: [
          { required: true, message: "标题名称不能为空", trigger: "blur" }
        ],
        daCarousel: [
          { required: true, message: "素材文件不能为空", trigger: "blur" }
        ],
        daDetails: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        agentLevel: [
          { required: true, message: "权限不能为空", trigger: "change" }
        ],
        isShow: [
          { required: true, message: "是否上线不能为空", trigger: "change" }
        ],
        numSoft: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        dataId: [
          { required: true, message: "资料类型不能为空", trigger: "blur" }
        ],
      },
      //获取类型数据
      aDataList:[],
      isFile: undefined,
      downTitle :"查看下载信息",
      downFlag :false,
      UserOperationList:[],//获取下载收藏分享点赞的信息
      userParams:{
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        phone: null,
        dataId: null,
        dataType: 2,
      },
      totals: 0,
      // 显示搜索条件
      showUserSearch: true,
      userDataId:"",
      dataPicker:[],
      updateTable:true,
    };
  },
  created() {
    this.getList();
    this.getDataList();
  },
  watch: {
    "form.daCarousel": {
      handler(val) {
        if (!val) {
          this.isFile = undefined;
        } else {
          let file_file = val.toLocaleString().split(',');
          for (let file of file_file) {
            this.isFile = isFile(file);
            if (this.isFile) {
              break
            }
          }
        }
      },
      deep: true,
      immediate: true
    },
  },
  methods: {
    /** 搜索按钮操作 */
    handleUserQuery() {
      this.userParams.pageNum = 1;
      this.queryList();
    },
    resetUserQuery(){
      this.userParams = {
        nickName: null,
        phone: null,
        dataId: null,
        dataType: 2,
      },
      this.queryList();
    },
    getUserOperationList(row){
      this.userDataId = row.id
      this.queryList();
    },
    queryList(){
      this.userParams.dataId = this.userDataId;
      getUserOperList(this.userParams).then(res =>{
        this.UserOperationList = res.rows;
        this.totals = res.total;
        this.downFlag = true;
      })
    },
    handleSwitchChange(row){
      updateADataInfo(row).then(res=>{
        if(res.code == 200){
          this.$modal.msgSuccess("修改成功");
          this.getList();
        }else{
          this.$modal.msgError(res.msg);
        }
      })
    },
    /** 查询素材库详情列表 */
    getList() {
      this.loading = true;
      if (this.dataPicker && this.dataPicker.length  && this.dataPicker[0] && this.dataPicker[1]){
        this.queryParams.params = {}
        this.queryParams.params.startTime = this.dataPicker[0] + " 00:00:00";
        this.queryParams.params.endTime = this.dataPicker[1] + " 23:59:59";
      }
      listADataInfo(this.queryParams).then(response => {
        this.aDataInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
       this.updateTable = !this.updateTable;
      });
    },
    getDataList(){
      listAData().then(response => {
        let treeData = handleTree(response.data);
        this.aDataList = this.handleDisable(treeData);
      });
    },
    handleDisable(data) {
      data.forEach(item =>{
        if(item.ancestors.split(",").length < 2){
          item.disabled = true;
        }
      });
      return data;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        daTitle: null,
        daCarousel: null,
        daDetails: null,
        agentLevel: null,
        likeNum: null,
        downloadNum: null,
        collectionNum: null,
        shareNum: null,
        dataId: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        isShow: null,
        numSoft: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params = {}
      this.dataPicker = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加素材库详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getADataInfo(id).then(response => {
        this.form = response.data;
        /*this.dataId = this.form.dataId;
        this.form.dataId = this.form.dataName;*/
        this.open = true;
        this.title = "修改素材库详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            /*if(this.form.dataName == this.form.dataId){
              this.form.dataId = this.dataId;
            }*/
            updateADataInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addADataInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除素材库详情编号为"' + ids + '"的数据项？').then(function() {
        return delADataInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/aDataInfo/export', {
        ...this.queryParams
      }, `aDataInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
